package service;

import dataAcsessObject.Exception.NotEntityFoundException;
import dataAcsessObject.JPADAOFactory;
import entity.Professor;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import service.exception.ObjectNotFoundException;
import valueObject.ProfessorVO;

/**
 *
 * @author Juan Hernández Serrato
 */
public class ProfessorService extends AbstractService<ProfessorVO, Professor> {

    @Override
    public void create(ProfessorVO valueObject, EntityManager em) {
        JPADAOFactory.getInstance().getProfessorDAO().create(valueObject.toEntity(), em);
    }

    @Override
    public void update(ProfessorVO valueObject, EntityManager em) throws ObjectNotFoundException {
        try {
            Professor professor = JPADAOFactory.getInstance().getProfessorDAO().findById(valueObject.getUserName(), em);
            professor = valueObject.toEntity();
            JPADAOFactory.getInstance().getProfessorDAO().update(professor, em);
        } catch (NotEntityFoundException ex) {
            throw new ObjectNotFoundException();
        }
    }

    @Override
    public void delete(Object id, EntityManager em) throws ObjectNotFoundException {
        try {
            Professor professor = JPADAOFactory.getInstance().getProfessorDAO().findById(id, em);
            JPADAOFactory.getInstance().getProfessorDAO().delete(professor, em);
        } catch (NotEntityFoundException ex) {
            throw new ObjectNotFoundException();
        }
    }

    @Override
    public ProfessorVO findById(Object id, EntityManager em) throws ObjectNotFoundException {
        try {
            return JPADAOFactory.getInstance().getProfessorDAO().findById(id, em).toValueObject();
        } catch (NotEntityFoundException ex) {
            throw new ObjectNotFoundException();
        }
    }

    @Override
    public List<ProfessorVO> findAll(EntityManager em) {
        List<ProfessorVO> professorVOs = new ArrayList<ProfessorVO>();
        List<Professor> professors = JPADAOFactory.getInstance().getProfessorDAO().findAll(em);
        for (Professor professor : professors) {
            professorVOs.add(professor.toValueObject());
        }
        return professorVOs;
    }

    @Override
    public List<ProfessorVO> findRange(int begin, int end, EntityManager em) {
        List<ProfessorVO> professorVOs = new ArrayList<ProfessorVO>();
        List<Professor> professors = JPADAOFactory.getInstance().getProfessorDAO().findRange(begin, end, em);
        for (Professor professor : professors) {
            professorVOs.add(professor.toValueObject());
        }
        return professorVOs;
    }
    
    public List<ProfessorVO> findProfessorsByUniversityUserName(String id, EntityManager em){
        List<ProfessorVO> professorVOs = new ArrayList<ProfessorVO>();
        List<Professor> professors = JPADAOFactory.getInstance().getProfessorDAO().findProfessorsByUniversityUserName(id, em);
        for (Professor professor : professors) {
            professorVOs.add(professor.toValueObject());
        }
        return professorVOs;
    }

}
